home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Fritz: All Fritz
/
All Fritz.zip
/
All Fritz
/
FILES
/
BUSINESS
/
STATA3.LZH
/
PROGRAMS.TUT
< prev
next >
Wrap
Text File
|
1988-08-26
|
5KB
|
157 lines
set output error
set display page 23
set more 1
#delimit ;
di _n(5) in wh
" ___ ____ ____ ____ ____ tm" _n
" /__ / ____/ / ____/" _n
"___/ / /___/ / /___/ Writing programs in Stata" _n
"---------------------------------------------------------"
_n(2) ;
di in gr
"In addition to all of its other features, Stata is programmable. Before ex-"
_n
"ploring Stata's programming language, let's emphsize those other features." _n
"You do not have to write programs to analyze data. The programming langugage"
_n
"is there for those who want or need it, but quite honestly, the majority of"
_n
"Stata users never write a program nor learn Stata's programming language."
_n(2)
"Among the group of users who do use Stata's programming language, most never"
_n
"use its most advanced features. They write short, one- or two-line programs"
_n
"that making using Stata interactively more convenient. Stata programs do not"
_n
"have to be long and involved, although they can be. When you write a program"
_n
"in Stata, you give it a name. That name becomes a new command that you use"
_n
"in the same way as you use any of Stata's built-in commands." _n ;
di in gr
"Finally, some users do write long and involved programs. They do this because"
_n
"there is some feature Stata is missing that is important in their work, or "
"they" _n
"do this because they want to automate a process that other users will run" _n
"on a regular basis." _n ;
#delimit cr
mac def path
capture run nullfile.tut
if _rc {
mac def path "\stata\"
capture run %path`nullfile.tut
if _rc {
mac def path "/usr/stata/"
capture run %path`nullfile.tut
if _rc {
#delimit ;
di in red
"I cannot find the other tutorial files. I have looked in the current" _n
"directory and in \stata (DOS) or /usr/stata (Unix). Is Stata installed" _n
"correctly?" _n(2)
"In any case, I cannot run the tutorial." ;
#delimit cr
exit
}
}
}
macro define F5 "do %path`contents.tut;"
macro define F6 "do %path`programs.tut;"
#delimit ;
drop _all ;
label drop _all ;
set more 0 ; more ; set more 1 ;
di _n (2) in wh
"Argument passing" _n
"----------------" _n(2) ;
di in gr
"Before you can begin programming in Stata, you must undertand how arguments"
_n
"are passed. They are passed through macros and you can learn about them by"
_n
"typing 'help macros' at the conclusion of this tutorial. In the meantime, it"
_n
"is enough to know that the first thing the user types after the program's name"
_n
"is referred to as " in wh "%" "_1" in gr ", the second thing as " in wh
"%" "_2" in gr ", and so on. This will make" _n
"sense shortly." _n(2)
"Let's assume that you have a data set were missing values are recorded as -9."
_n
"You wish to change all the -9's to Stata's missing value, which means you must"
_n
"type:" _n ;
di in wh _col(8) "replace " in gr "varname"
in wh "=. if " in gr "varname" in wh "==-9" _n(2)
in gr
"If you have ten such variables, typing that command ten times will be tedious."
_n(3) ;
set more 0 ; more ; set more 1 ;
di _n(2) in wh _dup(79) "-" _n in gr
"A program provides the solution:" _n(2)
_col(8) in wh
"program define fix" _n _col(8)
" replace %" "_1=. if %" "_1===9" _n _col(8)
"end" _n(2) in gr
"Typing this program into Stata creates a new command called "
in wh "fix" in gr ". Once the pro-" _n
"gram has been defined, if you type '" in wh "fix var1" in gr
"', Stata executes the command" _n
"'" in wh "replace var1=. if var1==9" in gr "'. Let's try it:" _n
in wh _dup(79) "-" _n(2)
". program define fix" _n
in bl " 1" in wh ". replace %" "_1=. if %" "_1==9" _n
in bl " 2" in wh ". end" _n ;
capture program drop fix ;
program define fix ;
replace %_1=. if %_1==-9;
end ;
di _n in wh _dup(79) "-" _n in gr
"Now all we need is some data. This tutorial is right now setting up data" _n
"for us to try our command." _n
in wh _dup(79) "-" _n(2)
". list" ;
drop _all ;
input x y z ;
1 4 -9 ;
-9 3 3 ;
1 -9 2 ;
5 5 -9 ;
end ;
set more 0 ; more ; set more 1 ;
noisily list ;
di _n in wh ". fix x" ;
noisily fix x ;
di _n in wh ". fix y" ;
noisily fix y ;
di _n in wh ". fix z" ;
noisily fix z ;
di _n in wh ". list" ;
set more 0 ; more ; set more 1 ;
noisily list ;
di _n(2) in wh _dup(79) "-" _n in gr
"It worked! We have just written our first Stata program. We could have made"
_n
"the program much fancier, but the majority of programs users write are no "
"more" _n
"complicated than " in wh "fix". _n
_dup(79) "-" _n ;
set more 0 ; more ; set more 1 ;
di _n(2) in wh _dup(79) "-" _n in gr
"You can learn more about Stata's programming language from the on-line help" _n
"files.
exit ;